let uid = 0;

export default class Dep {
    constructor () {
        this.id = uid++;
        this.subs = [];
    }

    addSub (sub) {
        this.subs.push(sub);
    }

    removeSub (sub) {
        remove(this.subs, sub);
    }

    depend () { // 依赖收集
        // 当响应式数据被其余地方引用时，如被监听时会触发数据的get，在get中进行依赖收集
        if (Dep.target && !this.subs.includes(Dep.target)) {
            this.addSub(Dep.target);
        }
    }

    notify () { // 通知更新
        // console.log(this.subs);
        const length = this.subs.length;
        for (let i = 0; i < length; i++) {
            console.log(this.subs);
            this.subs[i].updata();
        }
    }
}

function remove (arr, item) {
    if (arr.length) {
        const index = arr.indexOf(item);
        if (index > -1) return arr.splice(index, 1);
    }
}
